In this lecture, we'll explore how we can implement homing
behavior using an actor component. We'll start by watching the homing
behavior for both the teddy bears and the teddy bear projectiles. Again, I have sounds turned off so
I can talk about it and I'll play easy, and as you can see, periodically,
the teddy bears will start trying to chase the burger
instead of just moving randomly. And the projectiles also
will home towards us, you don't see that much when the teddy
bears are low on the screen. But if they're higher on the screen and
are particularly on higher difficulties, the teddy bear projectiles actually
come over towards the teddy bear, as you just saw there. It turns out that even though
we didn't need an interface for our event manager, we actually do
need an interface for homing actors. So any actor that's going to
have homing behavior needs to implement this homing actor interface. Basically, the actors need to
provide a GetStaticMesh() function, a GetImpulseForce() function and
a GetHomingDelay() function. The GetStaticMesh() function is so
that the homing component that we're going to look at soon can push
the static mesh in a new direction. The GetImpulseForce() function is so
that when the homing component does that, it applies the same magnitude of
impulse force that was originally applied to get our homing object
moving in the first place. And that makes it so
that it doesn't change speed, our homing object won't change speed,
it will just change direction. And we need to get the homing delay for
the actor because we don't implement this homing
behavior on every tick. We only periodically implement the homing
behavior, so on harder difficulties, the homing delay is shorter. So the homing component makes
the actor it's attached to home in more regularly
towards the burger. And that's how we can affect
the difficulty because actors that home more regularly are going to
follow the burger more closely. So any actor that's going to
have homing behavior needs to implement this interface. We haven't actually talked about actor
components since way back when we talked about manual timers in the second
course and the specialization. But this is a great place for
an actor component because we have multiple actors that we want
to exhibit the same behavior. And it's much better to just implement
that behavior in an actor component and then attach this actor component to any
actor we want to have that behavior. We're saving a pointer to
the burger pond because that will be our target for homing. We're going to save all
the information from the owner so that we don't have to
keep looking that up. And we'll use a homing timer that will
use that homing delay to decide when we should actually home in on something. We have a function that will move the
actor, that this component is attached to, toward the target. And then we have a function that will
figure out what force vector we should apply to do that. In our implementation file for
the homing actor component, on BeginPlay(), we find the burger so
that we can save it as a target. So we don't have to look for that burger
every time we're going to move toward it, and then we save that owner info. As we've seen before,
we can have a pointer to an interface, which we do here, because we know
that the teddy bear actor and the teddy bear projectile actor
will be using homing behavior but both of those classes will
implement this interface. So we just use a pointer to the interface,
After we get whoever owns us, so we'll get attached to an actor and
then we'll get that actor. And then we'll cast that
actor to the interface so that we can access these three
functions that we said any class that implements the interface has to implement. And then we start the homing timer. And when we do that,
we set it to the homing delay and we say when the timer finishes,
call the MoveTowardTarget() function. And then the MoveTowardTarget() function
does what we've seen in the Ted the Collector game,
where we stop moving and then we call the GetForceVector()
function that we'll look at in a moment. And we pass in the location
of the actor that owns this component in the location of the target,
the burger. And then, we add impulse to
the StaticMeshComponent of our owner to move our owner in
the direction of the burger. And then we start the timer again because
we're going to home in again after the homing delay. And this GetForceFactor()
is exactly the function that you saw me write when we
talked about Ted the Collector. In our teddy bear actor,
we need a couple of extra pound includes, we say that we're
implementing that interface. It will be more efficient for
us to save a couple values here, especially ImpulseForce
that we'll talk about soon. And we have a pointer to the homing
actor component that we're going to attach to this teddy bear actor. In the implementation file for
the teddy bear actor, on BeginPlay, we save
the StaticMeshComponent into a field so we don't have to look it up again to
implement our GetStaticMesh () function. And here's why we really need an impulse
force field here because we're generating a random impulse force and
we need to save that random impulse force. We can't just generate a new random
impulse force when the homing component wants to know what impulse force to use. We need to use the one we used
originally when we started the teddy bear actor movie. This is code that you saw back
when we did manual timers but here we're seeing it again. So we create a new home
in component object and we say we're going to be the owner
of that homing component. And we put it into our
HomingActorComponent field, we do our assert to make sure that worked properly
and then we register our component. And here are implementations of
the functions that are required by the interface. GetStaticMesh(), we'd just returned
are StaticMeshComponent field, GetImpulse Force,
we return our ImpulseForce field. And GetHomingDelay(),
we actually use our configuration data to get the bear homing delay because
we're in the TeddyBearActor here. The teddy bear projectile
works exactly the same way, except that in our implementation
of the GetHomingDelay() function, we get the bear projectile homing
delay from our configuration data. To recap in this lecture,
you saw how we could make teddy bears and teddy bear projectiles
exhibit homing behavior. You saw how cool actor components
are because we could give the same behavior to multiple objects. And even though I didn't talk about
it as I talked about the code, using an actor component is a much
better choice than using inheritance in this particular case because the homing
behavior is the only behavior that's shared by the teddy bear and
the teddy bear projectiles